var model = require('../models/commonModel'),
  _ = require('lodash'),
  errCode = require('../../conf/errorCode'),
  util = require('../helper/util'),
  cst = require('../../conf/const'),
  Log = require('../helper/logger'),
  tagLogger = new Log.TagLogger('COMMON_SERVICE');

// return: <string: errMsgs>, <integer: page>, <integer: size>
var parseIntPageSize = (exports.parseIntPageSize = function(page, size) {
  let errMsgs = null;
  if (page == null && size == null) {
    page = 1;
    size = cst.pageSize;
  } else {
    errMsgs = util.findInvalidKeyBy(
      {page, size},
      {
        page: page => {
          return util.isIntegerLike(page) && page > 0;
        },
        size: size => {
          return util.isIntegerLike(size) && size > 0;
        },
      },
    );
    if (errMsgs == null) {
      page = ~~page;
      size = ~~size;
    }
  }

  return [errMsgs, page, size];
});

exports.genUpdateCallback = function(callback) {
  return (err, rslt) => {
    if (err == null)
      callback(null, {affect: rslt.affectedRows, updated: rslt.changedRows});
    else callback(err, null);
  };
};
